Cscope
Cscope is a very powerful interface allowing you to easily navigate C-like code files. While Cscope comes with its own stand-alone interface, Vim provides the capability to navigate code without ever leaving the editor. Using Cscope, you can search for identifier or function definitions, their uses, or even any regular expression. With the proper configuration, "standard" include files of your compiler are automatically searched along with your sources. The output from says it all: cscope commands: add : Add a new database (Usage: add file|dir pre-path flags) find : Query for a pattern (Usage: find c|d|e|f|g|i|s|t name) c: Find functions calling this function d: Find functions called by this function e: Find this egrep pattern f: Find this file g: Find this definition i: Find files #including this file s: Find this C symbol t: Find assignments to help : Show this message (Usage: help) kill : Kill a connection (Usage: kill #) reset: Reinit all connections (Usage: reset) show : Show connections (Usage: show) Setting up Vim to use cscope Adding the following snippet to your .vimrc will set up Vim to use cscope more efficiently: if has('cscope') set cscopetag cscopeverbose if has('quickfix') set cscopequickfix=s-,c-,d-,i-,t-,e- endif cnoreabbrev csa cs add cnoreabbrev csf cs find cnoreabbrev csk cs kill cnoreabbrev csr cs reset cnoreabbrev css cs show cnoreabbrev csh cs help command -nargs=0 Cscope cs add $VIMSRC/src/cscope.out $VIMSRC/src endif Explanation: if has('cscope') If Vim hasn't got support for cscope, it's no use trying to use that support, so we bracket this whole snippet in this if statement to avoid unnecessary errors. set cscopetag cscopeverbose * on means Vim will include the cscope database whenever we search for a tag (e.g. by hitting < > in a C program). * on (optional) gives us a success/failure message when trying to add a cscope database (including the one near the end of this snippet). (only if compiled-in) specifies when to use for the output of cscope commands. We use the value given as example in the Vim help. cnoreabbrev Here we set up a number of command-line-mode to make cscope commands easier to type. These abbreviations can be made less intrusive by making sure they only trigger at the start of the command line, so that you can still type them normally in most cases where you don't actually want to use Cscope (for example, you may want to use css as a file extension sometimes). In Vim 7.0 or higher, this is easily done as follows: cnoreabbrev csa \ ((getcmdtype() ':' && getcmdpos() <= 4)? 'cs add' : 'csa') cnoreabbrev csf \ ((getcmdtype() ':' && getcmdpos() <= 4)? 'cs find' : 'csf') cnoreabbrev csk \ ((getcmdtype() ':' && getcmdpos() <= 4)? 'cs kill' : 'csk') cnoreabbrev csr \ ((getcmdtype() ':' && getcmdpos() <= 4)? 'cs reset' : 'csr') cnoreabbrev css \ ((getcmdtype() ':' && getcmdpos() <= 4)? 'cs show' : 'css') cnoreabbrev csh \ ((getcmdtype() ':' && getcmdpos() <= 4)? 'cs help' : 'csh') cs add We define a command :Cscope which will try to open the cscope database for the Vim source, and tell cscope that the relative paths in it are relative to the src directory containing the database itself (this assumes that the $VIMSRC variable has been set to the top directory of the Vim source. Using cscope commands This is the simplest: if you've forgotten the fine points, ':cs help''' (or, with the above abbreviations, :csh) will tell you. Generating the database Before you can use cscope on a set of source files, you must have a cscope database applying to them. You could set up commands in Vim to generate one (or more), or you can set up a script to do it outside of Vim. Another nice option would be to generate a new database from your makefile, to allow you to easily use all the same files that get compiled into your project. For instance, the following patch to the Vim src/Makefile adds a few targets related to generating a cscope database for the Vim source (and the "usual" include files, which cscope is clever enough to find): *** src/Makefile 2009-06-17 23:31:27.000000000 +0200 --- ../vim72/src/Makefile 2009-06-18 02:01:45.000000000 +0200 *************** *** 1734,1739 **** --- 1734,1748 tags TAGS: notags $(TAGPRG) $(TAGS_SRC) $(TAGS_INCL) + # Build the cscope database. + # This may search more files than necessary. + .PHONY: cscope csclean + csclean: + -rm -vf cscope.out + cscope.out: + cscope -bv ./*.ch ./*.cpp ./if_perl.xs auto/*.h auto/pathdef.c proto/*.pro + cscope: csclean cscope.out ; + # Make a highlight file for types. Requires Exuberant ctags and awk types: types.vim types.vim: $(TAGS_SRC) $(TAGS_INCL) Using these targets works best after compiling Vim, since it also scans files in auto/ which are generated by the configure+make run. See also Comments If you rebuild the cscope database while Vim has a cscope connection open, the new database won't be used until either (a) you kill and re-add the database, or (b) you use :cs reset --Tonymec 04:40, November 25, 2009 (UTC)